package com.hmk.blogboot.web;


import com.hmk.blogboot.po.Type;
import com.hmk.blogboot.service.BlogService;
import com.hmk.blogboot.service.TypeService;
import com.hmk.blogboot.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/*分页的页面的接口*/
@Controller
public class TypeShowController {
    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;

    //分页查询的页面展示在分页页面
    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size=8,sort={"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        @PathVariable Long id, Model model){
        //1分页查询 type列表
        List<Type> types = typeService.listTypeTop(10000);
        //判断typeId 是否为-1，如果为-1，是从首页跳转分页页面，默认选中分页列表第一条记录
        if(id==-1){
            id = types.get(0).getId();
        }

        //创建blogQuery对象
        BlogQuery blogQuery = new BlogQuery();
        //typeId进行设置到blogQuery对象 要用blogQuery中typeId进行查询获取list<Blog>
        blogQuery.setTypeId(id);
        //将数据添加model
        model.addAttribute("types",types);
        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));
        model.addAttribute("activeTypeId",id);
        return "types";
    }
}
